package com.yic.module.qms.service.inspectprocess;

import com.yic.framework.security.core.util.SecurityFrameworkUtils;
import com.yic.module.qms.service.inspectinfo.InspectInfoService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.qms.controller.admin.inspectprocess.vo.*;
import com.yic.module.qms.dal.dataobject.inspectprocess.InspectProcessDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.qms.convert.inspectprocess.InspectProcessConvert;
import com.yic.module.qms.dal.mysql.inspectprocess.InspectProcessMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.qms.enums.ErrorCodeConstants.*;

/**
 * 过程检验 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class InspectProcessServiceImpl implements InspectProcessService {

    @Resource
    private InspectProcessMapper inspectProcessMapper;
    @Resource
    private InspectInfoService inspectInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createInspectProcess(InspectProcessCreateReqVO createReqVO) {
        // 插入
        InspectProcessDO inspectProcess = InspectProcessConvert.INSTANCE.convert(createReqVO);
        inspectProcess.setUserId(SecurityFrameworkUtils.getLoginUserId());
        inspectProcessMapper.insert(inspectProcess);
        createReqVO.getEntryList().forEach(inspectInfoCreateReqVO -> {
            inspectInfoCreateReqVO.setInspectId(inspectProcess.getId());
            inspectInfoCreateReqVO.setInspectType(createReqVO.getInspectType());
        });
        inspectInfoService.createInspectInfo(createReqVO.getEntryList());
        // 返回
        return inspectProcess.getId();
    }

    @Override
    public void updateInspectProcess(InspectProcessUpdateReqVO updateReqVO) {
        // 校验存在
        validateInspectProcessExists(updateReqVO.getId());
        // 更新
        InspectProcessDO updateObj = InspectProcessConvert.INSTANCE.convert(updateReqVO);
        inspectProcessMapper.updateById(updateObj);
    }

    @Override
    public void deleteInspectProcess(Long id) {
        // 校验存在
        validateInspectProcessExists(id);
        // 删除
        inspectProcessMapper.deleteById(id);
    }

    private void validateInspectProcessExists(Long id) {
        if (inspectProcessMapper.selectById(id) == null) {
            throw exception(INSPECT_PROCESS_NOT_EXISTS);
        }
    }

    private void validateCreateOrUpdate() {
        // 检验数量不能超过生产数量
        // 如果检验类型为首检，校验数量只能为1，并且只能检验一次

    }

    @Override
    public InspectProcessDO getInspectProcess(Long id) {
        return inspectProcessMapper.selectById(id);
    }

    @Override
    public List<InspectProcessDO> getInspectProcessList(Collection<Long> ids) {
        return inspectProcessMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InspectProcessDO> getInspectProcessPage(InspectProcessPageReqVO pageReqVO) {
        return inspectProcessMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InspectProcessDO> getInspectProcessList(InspectProcessExportReqVO exportReqVO) {
        return inspectProcessMapper.selectList(exportReqVO);
    }

    @Override
    public List<InspectProcessDO> getInspectProcessList(Long moId, Long processId) {
        return inspectProcessMapper.selectListByMoAndProcess(moId, processId);
    }

}
